package org.example.socket.pojo;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class Request {
    private String method;

    private String url;

    private String host;

    private String port;

    private String path;

    // 请求头
    private HashMap<String, String> headers;

    private String version;

    // Map可以存两种类型
    // 考虑多个参数同名，value单个为字符串，多个为List
    private HashMap<String, Object> query;

    private String body;

    // 根据传输的文本构建request对象
    public Request(@NotNull String httpContent) {
        String[] lines = httpContent.split("\r\n");
        String[] firstLine = lines[0].split(" ");
        this.method = firstLine[0];
        this.url = firstLine[1];
        this.path = firstLine[1].split("\\?")[0];
        this.query = new HashMap<>();
        if (url.contains("?")) {
            String[] queryStrings = firstLine[1].split("\\?")[1].split("&");
            for (String queryString : queryStrings) {
                String[] keyValue = queryString.split("=");
                // 考虑多个参数同名，value单个为字符串，多个为List
                if (query.containsKey(keyValue[0])) {
                    Object OldValue = query.get(keyValue[0]);
                    List<String> newValues;
                    // 如果是字符串
                    if (OldValue.getClass().getName().equals("java.lang.String")) {
                        newValues = new ArrayList<>();
                        newValues.add(OldValue.toString());
                        newValues.add(keyValue[1]);
                    } else {
                        // noinspection unchecked
                        newValues = (List<String>) OldValue;
                        newValues.add(keyValue[1]);
                    }
                    query.put(keyValue[0], newValues);
                } else {
                    query.put(keyValue[0], keyValue[1]);
                }
            }
        }

        this.version = firstLine[2];
        this.headers = new HashMap<>();
        for (int i = 1; i < lines.length; i++) {
            String[] keyValue = lines[i].split(": ");
            if (keyValue.length == 2) {
                if (keyValue[0].equals("Host")) {
                    this.host = keyValue[1].split(":")[0];
                    this.port = keyValue[1].split(":")[1];
                } else {
                    this.headers.put(keyValue[0], keyValue[1]);
                }
            }
        }
        this.body = lines[lines.length - 1];
    }

    @Override
    public String toString() {
        return "Request{" +
                "method='" + method + '\'' +
                ", url='" + url + '\'' +
                ", host='" + host + '\'' +
                ", port='" + port + '\'' +
                ", path='" + path + '\'' +
                ", headers=" + headers +
                ", version='" + version + '\'' +
                ", query=" + query +
                ", body='" + body + '\'' +
                '}';
    }

    public String getMethod() {
        return method;
    }

    public String getUrl() {
        return url;
    }

    public String getHost() {
        return host;
    }

    public String getPort() {
        return port;
    }

    public String getPath() {
        return path;
    }

    public HashMap<String, Object> getQuery() {
        return query;
    }


    public HashMap<String, String> getHeaders() {
        return headers;
    }

    public String getVersion() {
        return version;
    }

    public String getBody() {
        return body;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public void setPort(String port) {
        this.port = port;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public void setHeaders(HashMap<String, String> headers) {
        this.headers = headers;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public void setQuery(HashMap<String, Object> query) {
        this.query = query;
    }

    public void setBody(String body) {
        this.body = body;
    }
}
